using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

/*

---------------------------------------------------------------------------
Values Validation  
---------------------------------------------------------------------------
Type{argumentsType}#(min; max)
Date{dmy}
Date{dmy}#[12/03/05; +)
Double{.}#(-; 13.7)
Integer
Integer#[3; 6]

---------------------------------------------------------------------------
OccursValidation 
--------------------------------------------------------------------------- 
(min, max)
[min, +)

*/

namespace Pampa.WebControls
{
    [ToolboxData("<{0}:Validator runat=server></{0}:Validator>")]
    [DesignerAttribute(typeof(BaseDesigner), typeof(System.ComponentModel.Design.IDesigner))]
    public class Validator : BaseControl
	{
        bool validateOnSubmit;
        List<KeyValuePair<string, Validation>> validations;

		public Validator()
		{
            this.validateOnSubmit = false;
            this.validations = new List<KeyValuePair<string, Validation>>();

            this.CssClass = "validatorDefault";
            this.clientClass = "Validator";
        }

        #region Properties

        public bool ValidateOnSubmit
        {
            get { return validateOnSubmit; }
            set { validateOnSubmit = value; }
        }

        public bool OnlyVisibles
        {
            get; set;
	    }
        
        #endregion

        #region Funciones para Agregar Validaciones

        public void AddRequiredValidation(string id, string message)
        {
            AddValidation(id, ValidationType.Required, null, message);
        }

        public void AddPatternValidation(string id, string args, string message)
        {
            AddValidation(id, ValidationType.Pattern, args, message);
        }

        public void AddRangeValidation(string id, string args, string message)
        {
            AddValidation(id, ValidationType.Range, args, message);
        }

        public void AddOccursValidation(string id, string args, string message)
        {
            AddValidation(id, ValidationType.Occurs, args, message);
        }

        public void AddCustomValidation(string id, string args, string message)
        {
            AddValidation(id, ValidationType.Custom, args, message);
        }

        public void AddValidation(string id, ValidationType type, string args, string message)
        {
            ValidateValidation(type, args);

            Validation newValidation = new Validation(type, args, message);
            AddValidation(id, newValidation);
        }

        public void AddValidation(string id, Validation validation)
        {
            this.validations.Add(new KeyValuePair<string, Validation>(id, validation));
        }


        #endregion

        #region Load

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            
            // registro el estilo y las librerias javascript en la pagina
            if (this.CssClass == "validatorDefault")
                RegisterStyleSheet("Pampa.WebControls/Validator.css");
            RegisterLibrary("Pampa.WebControls/Validator.js");
        }

        #endregion

        #region Render

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);

            RenderDefinitions(writer);
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.AddAttribute("id", "table_" + this.ID);
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderEndTag();
        }

        /// <summary>
        /// Construyo el objeto Customer que representa al combo
        /// </summary>
        private void RenderDefinitions(HtmlTextWriter output)
        {
            string objectName = "vld" + this.ID;

            output.WriteLine("<script type=\"text/javascript\">");
            output.WriteLine("var " + objectName + " = new Validator(\"" + this.ID + "\");");
            output.WriteLine(objectName + ".contentTableId = \"table_" + this.ID + "\";");
            output.WriteLine(objectName + ".onlyVisibles = " + this.OnlyVisibles.ToString().ToLower());

            if(this.ValidateOnSubmit)
            {
                output.WriteLine(objectName + ".validateOnSubmit(\"" + this.Page.Form.ID + "\");");
            }

            // Agrego las validaciones
            foreach (KeyValuePair<string, Validation> validDef in this.validations)
            {
                string id = validDef.Key;
                Validation validation = validDef.Value;
                string arguments = (String.IsNullOrEmpty(validation.Arguments)) ? "" : validation.Arguments.Replace("\\", "\\\\");
                output.WriteLine(objectName + ".addValidation(\"" + id + "\",\"" + validation.Type.ToString() + "\",\"" + arguments + "\",\"" + validation.Message + "\");");
            }

            output.WriteLine("</script>");
        }

        #endregion
        
        // TODO: Valida que los argumentos de la validacion sean correctos segun el tipo de validacion
        private void ValidateValidation(ValidationType type, string args)
        {
            switch (type)
            {
                case ValidationType.Range:
                    break;
                case ValidationType.Occurs:
                    break;
            }
        }
	}
}
